package com.uchiha.nova.mainService.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.model.entity.NavMenuEntity;
import com.uchiha.nova.core.model.vo.NavMenuVO;
import com.uchiha.nova.core.model.vo.RoleSelectVO;
import com.uchiha.nova.core.model.vo.TenantNavMenuSelectVO;
import com.uchiha.nova.core.model.query.NavMenuQuery;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.service.INavMenuService;
import com.uchiha.nova.mainService.wrapper.NavMenuWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 侧边栏 控制器
 *
 * @author magic
 */
@RestController
@AllArgsConstructor
@RequestMapping("/navMenu")
@Tag(name = "侧边栏")
@Slf4j
public class NavMenuController extends BaseController {

   @Resource
   INavMenuService navMenuService;

	/**
	 * 详情
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_DETAIL)
	@Operation(summary = "详情")
	public R<NavMenuVO> detail(@RequestBody NavMenuEntity navMenuEntity) {
		NavMenuEntity detail = navMenuService.getOne(Condition.getQueryWrapper(navMenuEntity));
		return R.data(NavMenuWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping(NovaCRUDMethodConstant.NOVA_LIST)
    @Operation(summary = "分页查询")
	public R getPage(NavMenuQuery query, QueryContext queryContext) {
		IPage<NavMenuEntity> pages = navMenuService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext,NavMenuEntity.class));
		return R.data(NavMenuWrapper.build().pageVO(pages));
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_SAVE)
	@Operation(summary = "新增")
	public R save(@RequestBody NavMenuEntity navMenuEntity) {
		return R.status(navMenuService.save(navMenuEntity));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_UPDATE)
	@Operation(summary = "修改")
	public R update(@RequestBody NavMenuEntity navMenuEntity) {
		return R.status(navMenuService.updateById(navMenuEntity));
	}

	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_SAVEORUPDATE)
	@Operation(summary = "新增或修改")
	public R saveOrUpdate(@RequestBody NavMenuEntity navMenuEntity) {
		return R.status(navMenuService.saveOrUpdate(navMenuEntity));
	}


	/**
	 * 删除 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_REMOVE)
	@Operation(summary = "逻辑删除")
	public R remove(@RequestParam String ids) {
		return R.status(navMenuService.deleteLogic(Func.toLongList(ids)));
	}
  
    /**
	 * 物理删除，不可恢复
	 */
   @Operation(summary = "物理删除")
   @DeleteMapping(NovaCRUDMethodConstant.NOVA_DELETE)
   public R delete(@RequestParam String ids){

     return R.status(navMenuService.deleteAll(Func.toLongList(ids)));
   }

	@Operation(summary = "构建树状")
	@GetMapping("/getTree")
	public R getTree(Long parentId){
		return R.data(navMenuService.getTree(parentId));
	}

	@Operation(summary = "获取全部树状")
	@GetMapping("/treeAll")
	public R treeAll(){
		return R.data(navMenuService.getTreeAll());
	}


	@Operation(summary = "获取全部树状")
	@GetMapping("/treeDict")
	public R treeDict(){
		return R.data(navMenuService.treeDict());
	}

	@Operation(summary = "租户获取全部树状")
	@GetMapping("/tenant/treeAll")
	public R treeAll(String roleName){
		return R.data(navMenuService.getTreeTenantAll(roleName));
	}

	@Operation(summary = "角色已经选择")
	@GetMapping("/roleHasSelect")
	public R roleHasSelect(Long roleId){
		return R.data(navMenuService.getRoleHasSelect(roleId));
	}

	@Operation(summary = "租户已经选择")
	@GetMapping("/tenantHasSelect")
	public R tenantHasSelect(String tenantId){
		return R.data(navMenuService.tenantHasSelect(tenantId));
	}

	@Operation(summary = "保存角色选择")
	@PostMapping("/roleSelect")
	public R roleSelect(@RequestBody RoleSelectVO roleSelectVO){
		return R.data(navMenuService.saveRoleSelectVO(roleSelectVO));
	}

	@Operation(summary = "保存租户选择")
	@PostMapping("/tenantNavMenuSelect")
	public R tenantNavMenuSelect(@RequestBody TenantNavMenuSelectVO tenantNavMenuSelectVO){
		return R.data(navMenuService.saveTenantSelectVO(tenantNavMenuSelectVO));
	}

	@Operation(summary = "租户可以选择")
	@GetMapping("/tenantCanSelect")
	public R tenantCanSelect(@RequestParam String tenantId){
		return R.data(navMenuService.tenantCanSelect(tenantId));
	}
}
